var mysql      = require('E:/App/Node/node_modules/mysql');
var config = {
    host     : '192.168.100.72',
    port     :'3306',
    user     : 'matrix_test',
    password : 'danlu@2015',
    database : 'matrix_test'
};

/*exports.getAppDao=function() {
    connection.connect();

    var sql='select app.app_id,app.name from app ';
    connection.query(sql, function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log(results);

    });
    connection.end();

}

function getConfigNameDao(version,appId) {
    connection.connect();

    var sql='select config.config_id,config.name from config where version=? and app_id=? ';
    var sqlParams=[version,appId];
    connection.query(sql, sqlParams,function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log(results);

    });
    connection.end();

}

function getConfigValueDao(version,appId,configId) {
    connection.connect();

    var sql='select config.value from config where version=? and app_id=? and config_id=?';
    var sqlParams=[version,appId,configId];
    connection.query(sql, sqlParams,function (error, results) {
        if(error){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }
        console.log(results);

    });
    connection.end();


}*/

var connection;

function handleDisconnect() {
    connection = mysql.createConnection(config)
    connection.connect(function (err) {
        if (err) {
            console.log("进行断线重连：" + new Date());
            setTimeout(handleDisconnect, 2000);   //2秒重连一次
            return;
        }
        console.log("连接成功");
    })
    connection.on('error', function (err) {
        console.log('db error', err);
        if (err.code === 'PROTOCOL_CONNECTION_LOST') {
            handleDisconnect();
        } else {
            throw err;
        }
    });
}
handleDisconnect()
exports.connection = connection
